home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / hgc32_12.zip / HGC32.C < prev    next >
C/C++ Source or Header  |  1995-01-29  |  11KB  |  381 lines

  1. /*+--------------------------------------------------------------+*/
  2. /*|                                                              |*/
  3. /*|     Source code for TDVIDEO.DLL (for TD32.EXE)               |*/
  4. /*|     for debugging 32-bit DOS programs (DPMI32) on systems    |*/
  5. /*|     with single monochrome monitor driven by Hercules        |*/
  6. /*|     graphics card.                                           |*/
  7. /*|                                                              |*/
  8. /*|     version 1.2 (c) 29.01.1995 Wojciech M. Zabolotny         |*/
  9. /*|     (WZAB@ipe.pw.edu.pl)                                     |*/
  10. /*|                                                              |*/
  11. /*|     This source is based on Help file for Borland's          |*/
  12. /*|     TDWINI.EXE (structure of TDVIDEO.DLL library)            |*/
  13. /*|                                                              |*/
  14. /*|     Many thanks to all the people who helped me to discover  |*/
  15. /*|     how to detect graphics modes of HGC card.                |*/
  16. /*|                                                              |*/
  17. /*+--------------------------------------------------------------+*/
  18.  
  19. #include <windows.h>
  20. #include <dos.h>
  21. #include <stdio.h>
  22. #include <conio.h>
  23.  
  24. /* Definitions of Hercules card's registers */
  25. #define HGC_Index         0x3b4
  26. #define HGC_Data          0x3b5
  27. #define HGC_Mode          0x3b8
  28. #define HGC_LightPenSet   0x3b9
  29. #define HGC_LightPenReset 0x3bb
  30. #define HGC_Status        0x3ba
  31. #define HGC_Config        0x3bf
  32. #define HGC_LightPenExt   0x3bf
  33.  
  34. WORD selB000 = 0;          /* selector of HGC's screen memory */
  35. WORD sel0040 = 0;          /* selector of BIOS data segment */
  36.  
  37. /*====================================================================*/
  38. /* Block of functions and variables used to save and restore BIOS     */
  39. /* state (size and location of cursor).                               */
  40. /*====================================================================*/
  41. unsigned char cRow,cCol,cStart,cEnd;
  42.  
  43. void SaveBiosCursor(void)
  44. {
  45.         asm {
  46.             mov ah,0x03
  47.             mov bh,0
  48.             int 0x10
  49.             mov cRow,dh
  50.             mov cCol,dl
  51.             mov cStart,ch
  52.             mov cEnd,cl
  53.             }
  54. }
  55.  
  56. void RestoreBiosCursor(void)
  57. {
  58.         asm {
  59.                 mov ah,01
  60.                 mov ch,cStart
  61.                 mov cl,cEnd
  62.                 int 0x10      /* Size of cursor restored */   
  63.                 mov ah,02
  64.                 mov bh,0
  65.                 mov dh,cRow
  66.                 mov dl,cCol
  67.                 int 0x10      /* Location of cursor restored */
  68.                 }
  69. }
  70.  
  71. /* This function sets BIOS flag at 0x40:0x65 to the last value */
  72. /* written to HGC's display's mode register                    */
  73.  
  74. void SetVMode (unsigned char vmode)
  75. {
  76. asm {
  77.         push es
  78.         mov ax,sel0040
  79.         mov es,ax
  80.         mov al,vmode
  81.         mov [byte ptr ES:0x65],al
  82.         pop es
  83.         }
  84. return;
  85. }
  86.  
  87. /*====================================================================*/
  88. /* Block of functions and variables used to save and restore video    */
  89. /* memory                                                             */
  90. /*====================================================================*/
  91.  
  92. /* Buffer for user's program video memory contents */
  93. unsigned char OldVMem[0x1000]; /* 4kB */
  94.  
  95. void RestoreVideoMemory(void)
  96. {
  97. /* This function restores previous video memory contents, saved */
  98. /* by SaveVideoMemory function                                  */ 
  99.     asm {
  100.     push es
  101.     push ds
  102.     push esi
  103.     push edi
  104.     mov ax,selB000
  105.     mov es,ax
  106.     xor edi,edi
  107.     lea esi,OldVMem
  108.     cld
  109.     mov ecx,1024   /* 4kB/4 */
  110.     rep movsd
  111.     pop edi
  112.     pop esi
  113.     pop ds
  114.     pop es
  115.     } 
  116. }
  117.  
  118. void SaveVideoMemory(void)
  119. /* Save previous video memory contents */
  120. {
  121.     asm {
  122.     push es
  123.     push ds
  124.     push esi
  125.     push edi
  126.     mov ax,ds
  127.     mov es,ax
  128.     lea edi,[OldVMem]
  129.     mov ax,selB000
  130.     mov ds,ax
  131.     xor esi,esi
  132.     cld
  133.     mov ecx,1024  /* 4kB/4 */
  134.     rep movsd
  135.     pop edi
  136.     pop esi
  137.     pop ds
  138.     pop es
  139.     } 
  140. }
  141.  
  142. /*====================================================================*/
  143. /* Block of functions and variables used to save and restore state of */
  144. /* Hercules graphics card                                             */
  145. /*====================================================================*/
  146.  
  147. unsigned char HGCPage; /* Number of visible graphics page */
  148. unsigned char HGCGraph; /* 0 in text mode, 1 in graphics mode */
  149.  
  150. void GetHGCMode(void)
  151. {
  152.     unsigned char LpExt;
  153.     unsigned short int CharNum;
  154.     asm {
  155.             mov dx,HGC_Status
  156.             }
  157.     /* Wait one frame before mode detection. If detection is performed */
  158.     /* immediately (as it was in version 1.0), wrong mode can be       */
  159.     /* detected when breakpoint is set near after "closegraph" or      */
  160.     /* "restorecrtmode" function call.                                 */
  161.     wait_novsync:
  162.     asm {
  163.             in al,dx
  164.             and al,0x80
  165.             je wait_novsync /* If VSYNC pulse has already begun, wait */
  166.                                                                             /* until it finishes                      */
  167.             }
  168.     wait_vsync:
  169.     asm  {
  170.             in al,dx
  171.             and al,0x80
  172.             jne wait_vsync: /* Wait for the beginning of VSYNC pulse  */
  173.             } 
  174.     /* Frame completed, detect graphics mode */
  175.     wait_novsync2:
  176.     asm {
  177.             in al,dx
  178.             and al,0x80
  179.             je wait_novsync2 /* If VSYNC pulse has already begun, wait */
  180.                                                                                 /* until it finishes                      */
  181.             }
  182.     wait_vsync2:
  183.     asm  {
  184.             in al,dx
  185.             and al,0x80
  186.             jne wait_vsync2: /* Wait for the beginning of VSYNC pulse */
  187.             /* Use light pen's trigger to obtain number of character  */
  188.             /* displayed at the beginning of VSYNC pulse, and infor-  */
  189.             /* mation about the visible graphics page                 */
  190.             mov dx,HGC_LightPenReset
  191.             out dx,al
  192.             mov dx,HGC_LightPenSet
  193.             out dx,al
  194.             mov dx,HGC_LightPenExt 
  195.             in al,dx    
  196.             mov LpExt,al
  197.             mov dx,HGC_Index
  198.             mov al,16
  199.             out dx,al
  200.             mov dx,HGC_Data
  201.             in al,dx
  202.             mov ah,al
  203.             mov dx,HGC_Index
  204.             mov al,17
  205.             out dx,al
  206.             mov dx,HGC_Data
  207.             in al,dx
  208.             mov CharNum,ax 
  209.             }
  210.     /* If the number of character displayed at the beginning of VSYNC    */
  211.     /* pulse is greater then 0xc000 then it is graphics mode             */
  212.         HGCGraph = (CharNum >= 0xc00) ? 1 : 0;
  213.     /* Number of graphics page is available as bit 7 of Light Pen Exten- */
  214.     /* sion register                                                     */
  215.         HGCPage = (LpExt & 0x80) ? 1 : 0;
  216. }
  217.  
  218. void SetHGCMode(unsigned char GraphFlag,unsigned char Page)    
  219. /* If GraphFlag != 0 this function switches graphics mode on and sets */
  220. /* visible page according to Page parameter. If GraphFlag == 0, this  */  
  221. /* function switches on the text mode.                                */ 
  222. {
  223.     int i,mode;
  224.     unsigned char vmode;
  225.     static unsigned char CRTC[2][12] ={
  226.             {0x61,0x50,0x52,0x0f,0x19,0x06,0x19,0x19,0x02,0x0d,0x0b,0x0c},
  227.             {0x35,0x2d,0x2e,0x07,0x5b,0x02,0x57,0x57,0x02,0x03,0x00,0x00 }
  228.             };
  229.     if(GraphFlag) {
  230.             mode=1;
  231.             outportb( HGC_Config, 3 );
  232.             if(Page==1) vmode=0x8a;
  233.             else        vmode=0x0a;
  234.             }
  235.     else {
  236.             mode=0;
  237.             outportb( HGC_Config, 0 );
  238.             vmode=0x29;
  239.             }
  240.     outportb( HGC_Mode,   vmode );
  241.     for ( i=0; i < 12; i++ ) {  
  242.             outportb( HGC_Index, i );
  243.             outportb( HGC_Data, CRTC[mode][i] );
  244.             };
  245.     SetVMode(vmode);
  246. };
  247.  
  248. /*====================================================================*/
  249. /* Block of functions required for DLL library                        */
  250. /*====================================================================*/
  251.  
  252. /*--------------------------------------------------------------------------*/
  253. #pragma argsused
  254. int FAR PASCAL LibMain( HANDLE h, WORD wDataSegment,
  255.             WORD wHeapSize, LPSTR lpszCmdLine )
  256. {
  257.             return 1;   /* Return success */
  258. }
  259. /*--------------------------------------------------------------------------*/
  260. #pragma argsused
  261. int FAR PASCAL WEP ( int bSystemExit )
  262. {
  263.  
  264.             return 1;  /* Return success */
  265. }
  266.  
  267. /*====================================================================*/
  268. /* Block of functions required for TDVIDEO.DLL library                */
  269. /*====================================================================*/
  270.  
  271. /*--------------------------------------------------------------------------*/
  272. WORD FAR PASCAL _export VideoInit (void)
  273. {
  274.     asm {
  275.         mov ax,2
  276.         mov bx,0x0040
  277.         int 0x31
  278.         jc Error
  279.         mov sel0040,ax
  280.         mov ax,2
  281.         mov bx,0xB000
  282.         int 0x31
  283.         jc Error
  284.         mov selB000,ax
  285.         }
  286.         printf("TDVIDEO.DLL for HGC v1.2 (c) 29.01.1995 W.M. Zabolotny\n");
  287.         return 0;  /* Return success */
  288.         Error:
  289.         return 5;  /* Error - it was imposible to obtain one of selectors */
  290. }
  291.  
  292. /*--------------------------------------------------------------------------*/
  293. WORD FAR PASCAL _export VideoDone (void)
  294. {
  295.         SetHGCMode(0,0);
  296.         clrscr();
  297.         return 1; /* Return success */
  298. }
  299.  
  300. /*--------------------------------------------------------------------------*/
  301. WORD FAR PASCAL _export VideoIsColor (void)
  302. {
  303.         return 0; /* Always mono monitor */
  304. }
  305.  
  306. /*--------------------------------------------------------------------------*/
  307. #pragma argsused
  308. WORD FAR PASCAL _export VideoGetTextSelector (int display)
  309. {
  310.         return selB000; /* Always mono monitor */
  311. }
  312.  
  313. /*--------------------------------------------------------------------------*/
  314. void FAR PASCAL _export VideoSetCursor (WORD y, WORD x)
  315. {
  316.         asm {
  317.                 mov ah,2
  318.                 mov bh,0
  319.                 mov dh,byte ptr y
  320.                 mov dl,byte ptr x
  321.                 int 0x10  
  322.                 /* Cursor moved to the location pointed by x and y */
  323.                 }
  324. }
  325.  
  326. /*--------------------------------------------------------------------------*/
  327. void FAR PASCAL _export VideoDebuggerScreen (void)
  328. {
  329. /* Save debugged program's screen */ 
  330.     GetHGCMode(); /* Save the previous video mode */
  331.     SaveVideoMemory();
  332.     SaveBiosCursor();
  333.     SetVMode(0x29);
  334.     SetHGCMode(0,0);
  335. }
  336.  
  337. /*--------------------------------------------------------------------------*/
  338. void FAR PASCAL _export VideoWindowsScreen (void)
  339. {
  340. /* Restore debugged program's screen */
  341.     SetHGCMode(HGCGraph,HGCPage);
  342.     RestoreVideoMemory();
  343.     RestoreBiosCursor();
  344. }
  345.  
  346. /*--------------------------------------------------------------------------*/
  347. WORD FAR PASCAL _export VideoBigSize (void)
  348. {
  349.         /* Screen height is always 25 lines. */
  350.         return 25;
  351. }
  352.  
  353. /*--------------------------------------------------------------------------*/
  354. #pragma argsused
  355. WORD FAR PASCAL _export VideoSetUp (WORD xtra1, WORD xtra2)
  356. {
  357.     /* This function does nothing */
  358.     return 0;
  359. }
  360.  
  361. /*--------------------------------------------------------------------------*/
  362. #pragma argsused
  363. void FAR PASCAL _export VideoSetSize (WORD bigflag)
  364. {
  365. /* Do nothing, screen size is never to be changed. */
  366. }
  367.  
  368. /*--------------------------------------------------------------------------*/
  369. #pragma argsused
  370. void FAR PASCAL _export VideoUpdateWindow (void)
  371. {
  372. /* Do nothing, Turbo Debugger displays all necessary information by itself. */
  373. }
  374.  
  375. /*--------------------------------------------------------------------------*/
  376. #pragma argsused
  377. void FAR PASCAL _export VideoConfig (HWND hWnd, HINSTANCE hInst, char *HelpFile)
  378. {
  379. /* Function not implemented, because HGC32.DLL can't work with TDWINI.EXE. */
  380. }
  381.